<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 2/23/15
 * Time: 9:32 PM
 */
namespace MyApp\Application\Controller;

use MyApp\Application\Model\ChiTieuModel;

class ChiTieuController extends MyAppAbstractController{
    public function __construct(){
        parent::__construct();
        parent::mustBeLogin();
    }

    public function indexAction(){
        $this->view->addHeaderScript('Public/plugin/autoNumberic/autoNumberic.js');
        $this->view->addHeaderScript('Public/js/ChiTieu/ChiTieu.js');


        $this->view->setTitle('Quản lý chi tiêu');

        $this->view->render('ChiTieu/index');
    }


    /*=========Ajax===============*/
    public function ThemChiTieuAction(){
        if (isset ($_POST['sotien'])){
            $model = new ChiTieuModel();
            $sotien = $_POST['sotien'];
            $lydo = $_POST['lydo'];

            echo json_encode($model->ThemChiTieu($sotien, $lydo));
        }
    }

    public function DanhSachChiTieuAction(){
        if (isset ($_POST['begin'])){
            $model = new ChiTieuModel();

            $begin = $_POST['begin'];
            $end = $_POST['end'];

            //$begin = date('Y-m-d', strtotime($begin)) . ' 00:00:00';
            //$end = date('Y-m-d', strtotime($end)) . ' 23:59:59';

            $begin = str_replace('/', '-', $begin);
            $begin = date('Y-m-d', strtotime($begin)) . ' 00:00:00';

            $end = str_replace('/', '-', $end);
            $end = date('Y-m-d', strtotime($end)) . ' 23:59:59';

            echo json_encode($model->DanhSachChiTieu($begin, $end));
        }
    }

    public function testAction(){
        $model = new ChiTieuModel();
        print_r($model->ChiTieuTheoNgay('2015-02-23','2015-02-24'));
    }
}